package com.zhq.controller;

import com.zhq.common.util.PageResult;
import com.zhq.common.util.R;
import com.zhq.entity.Goods;
import com.zhq.service.GoodsService;
import com.zhq.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

	@Autowired
	private GoodsService studentService;

	// 学生列表
	@RequestMapping("/list")
	public R getStudent() {
		return R.ok().data("list",studentService.list());
	}

	// 分页查询
	@GetMapping("/findByPage/{page}/{pageSize}")
	public R findByPage(@PathVariable("page") Integer page,
	                    @PathVariable("pageSize") Integer pageSize) {
		PageResult<Goods> pr = studentService.findByPage(page,pageSize);
		return R.ok().data("pr",pr);
	}
	// 条件带分页查询
	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable("page") Integer page,
	                @PathVariable("pageSize") Integer pageSize,
	                @RequestBody(required = false) GoodsVO vo){
		PageResult<Goods> pr = studentService.search(page,pageSize,vo);
		return R.ok().data("pr",pr);
	}

	// 添加学生
	@PostMapping("/add")
	public R add(@RequestBody Goods goods){
		boolean flag = studentService.add(goods);
		return flag ? R.ok().message("添加商品成功!") : R.error().message("添加商品失败!");
	}

	//修改学生
	@PutMapping("/update")
	public R update(@RequestBody Goods goods){
		boolean flag = studentService.updateById(goods);
		return flag ? R.ok().message("修改商品成功!") : R.error().message("修改商品失败!");
	}


	// 删除学生
	@DeleteMapping("/delete/{ids}")
	public R delete(@PathVariable("ids") String ids){  // 1,2,3
		String[] split = ids.split(",");
		List<String> sids = Arrays.asList(split);
		return studentService.removeByIds(sids) ? R.ok().message("删除商品成功!") : R.error().message("删除商品失败!");
	}
}
